package linkedList.删除链表的倒数第N个节点;

//给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。 
// 示例 1： 
//输入：head = [1,2,3,4,5], n = 2
//输出：[1,2,3,5]
// 示例 2： 
//输入：head = [1], n = 1
//输出：[]
// 示例 3： 
//输入：head = [1,2], n = 1
//输出：[1]
// 提示： 
// 链表中结点的数目为 sz 
// 1 <= sz <= 30 
// 0 <= Node.val <= 100 
// 1 <= n <= sz 
// 进阶：你能尝试使用一趟扫描实现吗？ 
// Related Topics 链表 双指针 👍 3151 👎 0


import java.util.List;

//leetcode submit region begin(Prohibit modification and deletion)
 class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
/**
 * @author zxl
 */
public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null){
            return head;
        }
        int del = del(head, n );
        if (del == 1){
            return head.next;
        }
        return head;
    }
    public int del(ListNode current , int n){
        if (current == null ){
            return n--;
        }
        n = del(current.next  , n);
        if (1 == n-- ){
            del(current);
        }

        return n;
    }

    /**
     * 删除当前元素
     * @param current
     */
    public void del(ListNode current){
        if (current == null ){
            return;
        }
        ListNode next = current.next;
        if (next == null){
            return;
        }
        current.next = next.next;
    }

    public static void main(String[] args) {
//        ListNode head = new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5,new ListNode(6))))));
        ListNode head = new ListNode(1);
        Solution solution = new Solution();
        ListNode listNode = solution.removeNthFromEnd(head, 1);

    }
}
